<template>
  <div class="decoration-wrap">
    <svg :width="`${width}px`" :height="`${height}px`">
      <rect :x="x" :y="y" :width="w" :height="h" :fill="colors[0]">
        <animate
          :attributeName="reverse ? 'height' : 'width'"
          from="0"
          :to="reverse ? height : width"
          :dur="`${dur}s`"
          calcMode="spline"
          keyTimes="0;1"
          keySplines=".42,0,.58,1"
          repeatCount="indefinite"
        />
      </rect>

      <rect :x="x" :y="y" width="1" height="1" :fill="colors[1]">
        <animate
          :attribute-name="reverse ? 'y' : 'x'"
          from="0"
          :to="reverse ? height : width"
          :dur="`${dur}s`"
          calc-mode="spline"
          key-times="0;1"
          key-splines="0.42,0,0.58,1"
          repeat-count="indefinite"
        />
      </rect>
    </svg>
  </div>
</template>

<script>
export default {
  name: 'HzxDecoration',
  props: {
    colors: {
      type: Array,
      default: () => (['#3faacb', '#fff'])
    },
    width: {
      type: Number,
      default: 50
    },
    height: {
      type: Number,
      default: 150
    },
    reverse: {
      type: Boolean,
      default: true
    },
    dur: {
      type: Number,
      default: 6
    }
  },
  data() {
    return {
      ref: 'decoration-2',
      x: 0,
      y: 0,
      w: 0,
      h: 0
    }
  },
  watch: {
    reverse() {
      this.calcSVGData()
    }
  },
  mounted() {
    this.calcSVGData()
  },
  methods: {
    calcSVGData() {
      if (this.reverse) {
        this.w = 1
        this.h = this.height
        this.x = this.width / 2
        this.y = 0
      } else {
        this.w = this.width
        this.h = 1
        this.x = 0
        this.y = this.height / 2
      }
    }
  }
}
</script>
<style lang="less" scoped="scoped">
    .decoration-wrap {
      width: 100%;
      height: 100%;
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
    }
</style>
